package com.example.administrator.jingpingou.application;

import android.app.Application;
import android.graphics.Bitmap;

import com.activeandroid.ActiveAndroid;
import com.activeandroid.Configuration;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.example.administrator.jingpingou.bean.LoginUserBean;
import com.example.administrator.jingpingou.bean.MineCollectBean;

import org.xutils.cache.LruCache;
import org.xutils.x;

/**
 * Created by Administrator on 2016/8/29 0029.
 */
public class MyApplication extends Application {
    private String link;

    public String getLink() {
        return link;
    }

    public void setLink(String link) {
        this.link = link;
    }

    private static RequestQueue mRequestQueue;
    private  static LruCache<String ,Bitmap> mLruCache;
    private LoginUserBean userBeen;
    private static boolean flag=false;

    private static String userName;

    public static String getUserName() {
        return userName;
    }

    public static void setUserName(String userName) {
        MyApplication.userName = userName;
    }

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    public LoginUserBean getUserBeen() {
        return userBeen;
    }

    public void setUserBeen(LoginUserBean userBeen) {
        this.userBeen = userBeen;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        userBeen =new LoginUserBean();
        x.Ext.init(this);
//        Configuration config = new Configuration.Builder(this).addModelClass(MineCollectBean.class).create();
//        ActiveAndroid.initialize(config);
        if (mRequestQueue == null){
            mRequestQueue = Volley.newRequestQueue(this);
        }
        if (mLruCache==null){
            long freeMemory = Runtime.getRuntime().freeMemory();
            mLruCache = new LruCache<String, Bitmap> ((int) freeMemory/8){
                @Override
                protected int sizeOf(String  key, Bitmap value) {
                    // 计算每一个bitmap的大小
                    return value.getRowBytes()*value.getHeight();
                }
            };
        }
        // 初始化
        //ActiveAndroid.initialize(this);
        Configuration configuration = new Configuration.Builder(this).setModelClasses(MineCollectBean.class).create();
        ActiveAndroid.initialize(configuration);
    }

    public static LruCache < String ,Bitmap> getLruCache(){
        return  mLruCache;
    }

    public static RequestQueue getRequestQueue (){
        return  mRequestQueue;
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        mRequestQueue = null;
        //注销
        ActiveAndroid.dispose();
    }
    //unicode转化utf-8
    public  String decodeUnicode(String theString) {
        char aChar;
        int len = theString.length();
        StringBuffer outBuffer = new StringBuffer(len);
        for (int x = 0; x < len; ) {
            aChar = theString.charAt(x++);
            if (aChar == '\\') {
                aChar = theString.charAt(x++);
                if (aChar == 'u') {
                    // Read the xxxx
                    int value = 0;
                    for (int i = 0; i < 4; i++) {
                        aChar = theString.charAt(x++);
                        switch (aChar) {
                            case '0':
                            case '1':
                            case '2':
                            case '3':
                            case '4':
                            case '5':
                            case '6':
                            case '7':
                            case '8':
                            case '9':
                                value = (value << 4) + aChar - '0';
                                break;
                            case 'a':
                            case 'b':
                            case 'c':
                            case 'd':
                            case 'e':
                            case 'f':
                                value = (value << 4) + 10 + aChar - 'a';
                                break;
                            case 'A':
                            case 'B':
                            case 'C':
                            case 'D':
                            case 'E':
                            case 'F':
                                value = (value << 4) + 10 + aChar - 'A';
                                break;
                            default:
                                throw new IllegalArgumentException(
                                        "Malformed   \\uxxxx   encoding.");
                        }

                    }
                    outBuffer.append((char) value);
                } else {
                    if (aChar == 't')
                        aChar = '\t';
                    else if (aChar == 'r')
                        aChar = '\r';
                    else if (aChar == 'n')
                        aChar = '\n';
                    else if (aChar == 'f')
                        aChar = '\f';
                    outBuffer.append(aChar);
                }
            } else
                outBuffer.append(aChar);
        }
        return outBuffer.toString();
    }


}
